import axios from 'axios'
import { Notification } from 'element-ui'
import store from "@/store";
import {getToken} from "@/utils/auth";
const service = axios.create({
  baseURL: 'http://r2.hx-im.com.cn/api/v1', // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 50000 // request timeout
})

service.interceptors.request.use(
  config => {
    // do something before request is sent
    if (store.getters.token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers['Content-Type'] = 'application/json'
      // console.log(config.headers['token'])
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

service.interceptors.response.use(response => {
  // console.log("TCL: response", response)
  const res = response.data
  if (res.error_code === 888) {
    Notification({
      title: res.message || 'error',
      type: 'error'
    })
    return Promise.reject(res.message || 'error')
  }
  return res
}, error => {
  Notification({
    title: error.message,
    type: 'error'
  })
  return Promise.reject(error)
})
export default service
